<template>
  <div class="log-box-style">
    <div class="query-form-style">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="文件名" prop="fileName">
          <el-input v-model="queryParams.fileName" placeholder="请输入文件名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="handleResetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table ref="adminTable" :data="resultList" style="width: 100%;" border>
      <el-table-column label="文件名" prop="fileName" align="center" min-width="100"></el-table-column>
      <el-table-column label="文件长度" prop="fileLength" align="center" min-width="100"></el-table-column>
      <el-table-column label="文件类型" prop="fileType" align="center" min-width="100"></el-table-column>
      <el-table-column label="文件全路径" prop="fullPath" align="center" min-width="100"></el-table-column>
      <el-table-column label="图片" prop="fullPath" align="center" min-width="100">
        <template slot-scope="scope">
          <viewer> <img style="width: 100px; height: 100px" :src="scope.row.fullPath" /> </viewer>
        </template>
      </el-table-column>
      <el-table-column label="对象ID" prop="objId" align="center" min-width="100"></el-table-column>
      <el-table-column label="数据来源" prop="sourceType" align="center" min-width="100"></el-table-column>
      <el-table-column label="上传时间" width="160" align="center">
        <template slot-scope="scope">{{scope.row.createTime | formatDateTime}}</template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="text"
                     @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :page-sizes="[10,15,20, 50, 100]"
        :total="resultTotal">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {formatDate} from '@/utils/date';

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  fileName: null,
};

const defaultForm = {
  id: undefined,
  fileLength: undefined,

}

export default {
  name: "index",
  data() {
    return {
      queryParams: Object.assign({}, defaultListQuery),
      resultList: [],
      resultTotal: 0,
      // 日期范围
      dateRange: [],
      form: Object.assign({}, defaultForm),
    }
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === '') {
        return 'N/A';
      }
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  created() {
    this.handleQuery();
  },
  methods: {
    /** 搜索 + 分页 */
    handleResetSearch() {
      this.dateRange = [];
      this.queryParams = Object.assign({}, defaultListQuery);
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      this.resultList = [];
      let dateRange = Array.isArray(this.dateRange) ? this.dateRange : [];
      this.queryParams.endTime = dateRange[0];
      this.queryParams.endTime = dateRange[1];
      this.$commonList("/storage/list", "GET", this.queryParams).then(res => {
        if (res.code === 200) {
          this.resultList = res.data.list;
          this.resultTotal = res.data.total;
        }
      })
    },
    handleSizeChange(val) {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    handleDelete(index, row) {
      this.$confirm('是否要删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$commonList(`/storage/deleteByObjId/${row.objId}`, "DELETE").then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        })
      });
    },
    handleUpdate(index, row) {
      this.dialogVisible = true;
      this.isEdit = true;
      this.form = Object.assign({}, row);
    },
  },
}
</script>

<style lang="scss" scoped>
.log-box-style {
  color: red;
  padding: 20px;
  .query-form-style {

  }
}
</style>
